#ifndef AD_H
#define AD_H

#include "system_config.h"
#include "event.h"
#include "device.h"


#define AD_PRECISION_WIDTH			(1<<AD_PRECISION)
#define AD_SAMPLE_VALUE_MIN			0
#define AD_SAMPLE_VALUE_MAX			(AD_PRECISION_WIDTH - 1)

#define AD_FROM_VOLT2(x)			((x)/2.0*AD_SAMPLE_VALUE_MAX)
#define AD_FROM_VOLT2P4(x)		((x)/2.4*AD_SAMPLE_VALUE_MAX)
#define AD_FROM_VOLT3(x)			((x)/3.0*AD_SAMPLE_VALUE_MAX)
#define AD_FROM_VOLT3P3(x)		((x)/3.3*AD_SAMPLE_VALUE_MAX)
#define AD_FROM_VOLT4(x)			((x)/4.0*AD_SAMPLE_VALUE_MAX)
#define AD_FROM_VOLT5(x)			((x)/5.0*AD_SAMPLE_VALUE_MAX)



#define AD_TO_VOLT2(x)				((x)*2.0/AD_SAMPLE_VALUE_MAX)
#define AD_TO_VOLT2P4(x)			((x)*2.4/AD_SAMPLE_VALUE_MAX)
#define AD_TO_VOLT3(x)				((x)*3.0/AD_SAMPLE_VALUE_MAX)
#define AD_TO_VOLT3P3(x)			((x)*3.3/AD_SAMPLE_VALUE_MAX)
#define AD_TO_VOLT4(x)				((x)*4.0/AD_SAMPLE_VALUE_MAX)
#define AD_TO_VOLT5(x)				((x)*5.0/AD_SAMPLE_VALUE_MAX)




void ad_init(event_t evt);
void ad_measure(unsigned char channel);
unsigned int ad_get_result();
unsigned int ad_measure_and_get_result(unsigned char channel);
void ad_close();

//backward compactibility
#define AD_Power_On()				ad_init(0)
#define AD_Convert(ch)			ad_measure_and_get_result(ch)
#define AD_Power_Off()			ad_close()

#endif